package com.qdsgjsfk.vision.ui.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.qdsgjsfk.vision.base.BaseDialogFragment;
import com.qdsgjsfk.vision.eventbus.ModeChange;
import com.qdsgjsfk.vision.model.ChartType;
import com.qdsgjsfk.vision.util.Constant;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.qdsgjsfk.vision.R;

import org.greenrobot.eventbus.EventBus;

import util.PreferencesHelper;

/**
 * Created by HP on 2019/7/18.
 */

public class VisionModelDialog extends BaseDialogFragment {

    public static VisionModelDialog newInstance() {
        Bundle args = new Bundle();
        VisionModelDialog fragment = new VisionModelDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private List<ChartType> list = new ArrayList<>();
    private MyAdapter myAdapter;
    private boolean isInit = true;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_vision_model, container);
        TextView btn_confirm = view.findViewById(R.id.btn_confirm);
        TextView title = view.findViewById(R.id.title);
        RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
        RadioGroup rgMode = view.findViewById(R.id.rg_mode);
        RadioButton rbtnModelDoctor = view.findViewById(R.id.rbtn_model_doctor);
        RadioButton rbtnModelSelf = view.findViewById(R.id.rbtn_model_self);
        myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));

        rgMode.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == rbtnModelDoctor.getId()) {
                title.setText("选择视力表");
                PreferencesHelper.getInstance().set(getActivity(), Constant.MODE, Constant.MODE_TEACHER);
                if (!isInit) {
                    PreferencesHelper.getInstance().set(getActivity(), Constant.CHART_TYPE, Constant.CHART_TEACHER_E);
                }
            } else {
                title.setText("选择操作方式");
                PreferencesHelper.getInstance().set(getActivity(), Constant.MODE, Constant.MODE_STUDENT);
                PreferencesHelper.getInstance().set(getActivity(), Constant.CHART_TYPE, Constant.CHART_STUDENT_NEW);
                if (!isInit) {
                    PreferencesHelper.getInstance().set(getActivity(), Constant.EQUIPMENT, Constant.EXTERNAL_EQUIPMENT_OPERATE);
                }
            }
            initList();
        });

        if (PreferencesHelper.getInstance().getInt(getActivity(), Constant.MODE, Constant.MODE_TEACHER) == Constant.MODE_TEACHER) {
            rbtnModelDoctor.setChecked(true);
        } else {
            rbtnModelSelf.setChecked(true);
        }

        btn_confirm.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onConfirm();
            }
            EventBus.getDefault().postSticky(new ModeChange(PreferencesHelper.getInstance().getInt(getActivity(), Constant.MODE, 1)));
            dismiss();
        });
        return view;
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
//            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//            window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog().getWindow()).requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm();

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    private void initList() {
        list.clear();
        if (PreferencesHelper.getInstance().getInt(getActivity(), Constant.MODE, Constant.MODE_TEACHER) == Constant.MODE_TEACHER) {
            ChartType chartType1 = new ChartType();
            chartType1.mode = Constant.MODE_TEACHER;
            chartType1.name = "标准对数视力表";
            chartType1.resource = R.mipmap.chart_type_e;
            chartType1.chartType = Constant.CHART_TEACHER_E;

            ChartType chartType2 = new ChartType();
            chartType2.mode = Constant.MODE_TEACHER;
            chartType2.name = "2.5m对数视力表";
            chartType2.resource = R.mipmap.chart_type_e;
            chartType2.chartType = Constant.CHART_TEACHER_E_2_5;

            ChartType chartType3 = new ChartType();
            chartType3.mode = Constant.MODE_TEACHER;
            chartType3.name = "C型对数视力表";
            chartType3.resource = R.mipmap.chart_type_c;
            chartType3.chartType = Constant.CHART_TEACHER_C;

            ChartType chartType4 = new ChartType();
            chartType4.mode = Constant.MODE_TEACHER;
            chartType4.name = "儿童对数视力表";
            chartType4.resource = R.mipmap.chart_type_animal;
            chartType4.chartType = Constant.CHART_TEACHER_ANIMAL;
            list.add(chartType1);
            list.add(chartType3);
            list.add(chartType4);
            list.add(chartType2);
            list.get(PreferencesHelper.getInstance().getInt(getActivity(), Constant.CHART_TYPE, 1) - 1).flag = true;
        } else {
            ChartType chartType1 = new ChartType();
            chartType1.mode = Constant.MODE_STUDENT;
            chartType1.name = "外接设备操作";
            chartType1.resource = R.mipmap.chart_type_o;
            chartType1.chartType = Constant.CHART_STUDENT_NEW;
            chartType1.equipmentType = Constant.EXTERNAL_EQUIPMENT_OPERATE;

            ChartType chartType2 = new ChartType();
            chartType2.mode = Constant.MODE_STUDENT;
            chartType2.name = "本机操作";
            chartType2.resource = R.mipmap.chart_type_m;
            chartType2.chartType = Constant.CHART_STUDENT_NEW;
            chartType2.equipmentType = Constant.LOCAL_EQUIPMENT_OPERATE;

            list.add(chartType1);
            list.add(chartType2);
            if (PreferencesHelper.getInstance().getInt(getActivity(), Constant.EQUIPMENT, 0) == Constant.EXTERNAL_EQUIPMENT_OPERATE) {
                list.get(0).flag = true;
            } else {
                list.get(1).flag = true;
            }
        }
        myAdapter.notifyDataSetChanged();
        isInit = false;
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @NonNull
        @Override
        public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new Holder(getLayoutInflater().inflate(R.layout.item_chart_type, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Holder holder, int position) {
            if (list.get(position).flag) {
                holder.tv_model.setTextColor(Color.parseColor("#3EBFC7"));
            } else {
                holder.tv_model.setTextColor(Color.parseColor("#cccccc"));
            }
            holder.tv_model.setText(list.get(position).name);
            holder.btn_model.setImageResource(list.get(position).resource);
            holder.ll_model.setOnClickListener(v -> {
                if (PreferencesHelper.getInstance().getInt(getActivity(), Constant.MODE, Constant.MODE_TEACHER) == Constant.MODE_TEACHER) {
                    PreferencesHelper.getInstance().set(getActivity(), Constant.CHART_TYPE, list.get(position).chartType);
                } else {
                    PreferencesHelper.getInstance().set(getActivity(), Constant.EQUIPMENT, position);
                }
                for (ChartType chartType : list) {
                    chartType.flag = false;
                }
                list.get(position).flag = true;
                notifyDataSetChanged();
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {
            TextView tv_model;
            ImageView btn_model;
            LinearLayout ll_model;

            Holder(View itemView) {
                super(itemView);
                tv_model = itemView.findViewById(R.id.tv_model);
                btn_model = itemView.findViewById(R.id.btn_model);
                ll_model = itemView.findViewById(R.id.ll_model);
            }
        }
    }
}
